//
//  BRPointsVC.m
//  Kexu
//
//  Created by IOS on 2023/7/28.
//

#import "BRPointsVC.h"
#import "BRPointRuleTbCell.h"
#import "BRWalletViewController.h"
#import "BRPointRecordVC.h"

@interface BRPointsVC ()<QMUITableViewDelegate,QMUITableViewDataSource>

@property (nonatomic,strong) QMUILabel *pointL;

@property (nonatomic,strong) NSMutableArray *dataList;

@property (nonatomic,assign) BOOL hasCard;

@end

@implementation BRPointsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataList = [NSMutableArray array];
    [self setUI];
    [self addRefreshActionForView:self.tableView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self reqestPoint];
    [self requePointRule];
}

- (void)handleListRefreshAction {
    [self reqestPoint];
    [self requePointRule];
}


#pragma mark - request --
- (void)reqestPoint {
    
    WS(weakSelf);
    [BRUserManager requestPoints:^{

        weakSelf.pointL.text = [NSString stringWithFormat:@"%ld",[BRUserManager share].point];
    } faile:^{
        
    }];
}


- (void)requePointRule{
    
   
    NSString *url = [BRAPI pointRule];
    [self.tableView ly_startLoading];
    [[BRRequestManager shared] GET:url params:@{} succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        NSLog(@"%@",dataDict);
        
        [self.dataList removeAllObjects];

        
        NSArray *list = [BRPointRuleModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        
        for (BRPointRuleModel *dto in list) {
            if (dto.state) {
                [self.dataList addObject:dto];
            }
        }
        [self.tableView reloadData];
        [self showBREmptyView:self.tableView];
        [self handleEndRefreshForView:self.tableView];
        
        [self req_cardState];
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        [self showBRErrorView:self.tableView];
        [self handleEndRefreshForView:self.tableView];
    }];
}

/// 获取用户是否绑卡
- (void)req_cardState {
    
    [[BRRequestManager shared] GET:@"/api/users/check/band/card" params:@{} succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
      
        NSArray *arr = (NSArray *)dataDict[@"data"];
        if (BR_ArrayIsEmpty(arr)) {
            return;
        }
        self.hasCard = arr.count != 0;
        [self.tableView reloadData];
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        
    }];
}


- (void)toPointHistory {
    BRPointRecordVC *vc = [[BRPointRecordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)typeAction:(NSInteger)type {
    if (type == 3) {
        // 回首页租借
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;
    }
    
    BRWalletViewController *vc = [[BRWalletViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - tableView Delegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return 5;
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRPointRuleTbCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRPointRuleTbCell"];
    BRPointRuleModel *model = self.dataList[indexPath.row];
    cell.dto = model;
    
    if (cell.type == 4) {
        cell.acBtn.hidden = self.hasCard;
    }
    
    WS(weakSelf);
    cell.actionBlock = ^(NSInteger type) {
        [weakSelf typeAction:type];
    };
   
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
}


#pragma mark - UI --
- (void)setUI {
    
    CGFloat content_y = 132 + kSafeAreaTopHeight + 8;
    
    BRNavView *top = [self br_createTop:Localized(@"menu_points")];
    top.mj_h = content_y;
    top.topBGView.backgroundColor = kUIColorMainTint;
    
    QMUILabel *pointL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontB:37]];
    [top.topBGView addSubview:pointL];
    [pointL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.bottom.offset(-28);
    }];
    _pointL = pointL;
    
    BRImgLabelView *pointV = [[BRImgLabelView alloc] init];
    [pointV.img mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(16);
    }];
    pointV.titleL.text = Localized(@"points_myPoints");
    pointV.img.image = UIImageMake(@"point_points");
    pointV.titleL.textColor = kColorTitle;
    
    [top.topBGView addSubview:pointV];
    [pointV mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.offset(28);
        make.bottom.equalTo(pointL.mas_top).offset(0);
    }];
    
    UIImageView *rImg = [[UIImageView alloc] initWithImage:UIImageMake(@"point_line")];
    [top.topBGView addSubview:rImg];
    [rImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(180);
        make.right.offset(0);
        make.bottom.offset(-28);
    }];
    
    QMUIButton *pointHbtn = [[QMUIButton alloc] init];
    [pointHbtn setTitle:Localized(@"points_pointHistory") forState:UIControlStateNormal];
    [pointHbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [pointHbtn setBackgroundColor:kColorMain];
    pointHbtn.layer.cornerRadius = 10;
    pointHbtn.layer.masksToBounds = YES;
    pointHbtn.titleEdgeInsets = UIEdgeInsetsMake(0, 14, 0, 14);
    pointHbtn.titleLabel.font = [UIFont fontS:14];
    [top.topBGView addSubview:pointHbtn];
    [pointHbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(36);
        make.right.offset(-20);
        make.bottom.offset(-42);
    }];
    [pointHbtn addTarget:self action:@selector(toPointHistory) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.frame = CGRectMake(0, content_y - 8, kScreenWidth, kScreenHeight - (content_y - 8));
    self.tableView.estimatedRowHeight = 134;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.tableView registerClass:[BRPointRuleTbCell class] forCellReuseIdentifier:@"BRPointRuleTbCell"];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 14)];
    self.tableView.tableFooterView = foot;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
